const util = require("../../utils/util");
var app = getApp();
Page({
    data: {
        current: '0',
        userInfo: null,
        isLogin: false,
        showDataList: [],

        readyDoList: [],
        readyDoPageIndex: 1,
        readyDoPageCount: 1,
        pageSize: 10,
        hadDoPageIndex: 1,
        hadDoPageCount: 1,
        hadDoList: [],
        allHadDo: 0,
        allReadDo: 0,
        pulldownrefresh: false,

        loading: true,

        selectId: 0,
        visibleShowPass: false,
        passActions: [{
            name: '通过',
            color: '#ed3f14'
        }],
        visibleShowRefuse: false,
        refuseActions: [
            {
                name: '取消'
            },
            {
                name: '驳回',
                color: '#ed3f14',
                loading: false
            }
        ],
        refuseContent: ''
    },
    onLoad: function (e) {
        //检查token是否存在
        var userInfo = util.getStorageSync("userInfo");
        if (userInfo != null) {
            this.setData({
                userInfo: app.globalData.userInfo,
                isLogin: true
            });
        }
    },
    getVerifyHistoryDetail: function (verifyState, pageIndex, pageSize, refresh) {
        var self = this;
        wx.showLoading({
            title: '数据加载中…'
        });
        // 请求数据，并渲染
        wx.request({
            url: app.globalData.api + '/CostNote/GetVerifyHistoryDetail',
            data: {
                token: app.globalData.userInfo.token,
                verifyState: verifyState,
                pageIndex: pageIndex,
                pageSize: pageSize
            },
            method: 'GET',
            success: function (res) {
                if (res.data.resultCode == 0) {
                    self.setData({
                        loading: false
                    });
                    //停止刷新
                    if (self.data.pulldownrefresh) {
                        wx.stopPullDownRefresh();
                    }
                    var arr = [];
                    if (!refresh) {
                        if (verifyState == 0) {
                            arr = self.data.readyDoList;
                        } else {
                            arr = self.data.hadDoList;
                        }
                    } else {
                        arr = [];
                        if (verifyState == 0) {
                            self.setData({
                                readyDoList: arr,
                                readyDoPageCount: 1
                            });
                        } else {
                            self.setData({
                                hadDoList: arr,
                                hadDoPageCount: 1
                            });
                        }
                    }
                    if (res.data.data.dataList && res.data.data.dataList.length > 0) {
                        res.data.data.dataList.forEach(element => {
                            element.dailyDate = util.dateUtil.format(new Date(element.dailyDate), 'Y-M-D');
                            arr.push(element);
                        });
                    }
                    self.setData({
                        allHadDo: res.data.data.statisticsModel.allHadDo,
                        allReadDo: res.data.data.statisticsModel.allReadDo,
                    });
                    if (verifyState == 0) {
                        self.setData({
                            readyDoList: arr,
                            readyDoPageCount: res.data.data.pageCount
                        });
                    } else {
                        self.setData({
                            hadDoList: arr,
                            hadDoPageCount: res.data.data.pageCount
                        });
                    }
                    if (self.data.current == '0') {
                        self.setData({
                            showDataList: self.data.readyDoList
                        });
                    } else {
                        self.setData({
                            showDataList: self.data.hadDoList
                        });
                    }
                } else {
                    wx.showToast({
                        title: res.data.message,
                        icon: 'none',
                        duration: 2000
                    })
                }
            },
            complete: () => {
                //停止刷新
                if (self.data.pulldownrefresh) {
                    wx.stopPullDownRefresh();
                }
                wx.hideLoading();
            }
        });
    },
    getReadyDoList: function (refresh) {
        let pageIndex = refresh ? 1 : this.data.readyDoPageIndex + 1;
        this.getVerifyHistoryDetail(0, this.data.readyDoPageIndex, this.data.pageSize, refresh);
        this.setData({
            readyDoPageIndex: pageIndex + 1
        });
    },
    getHadDoList: function (refresh) {
        this.getVerifyHistoryDetail(1, this.data.hadDoPageIndex, this.data.pageSize, refresh);
        this.setData({
            hadDoPageIndex: this.data.hadDoPageIndex + 1
        });
    },
    onShow: function () {
        if (!this.data.isLogin) {
            return;
        }
        this.initPageData();
    },
    onPullDownRefresh: function () {
        this.initPageData();
    },
    initPageData: function () {
        this.setData({
            readyDoList: [],
            readyDoPageIndex: 1,
            readyDoPageCount: 1,
            pageSize: 10,
            hadDoPageIndex: 1,
            hadDoPageCount: 1,
            hadDoList: [],
            allHadDo: 0,
            allReadDo: 0,
            pulldownrefresh: true
        });
        if (!this.data.isLogin) {
            return;
        }
        this.getReadyDoList(true);
        this.getHadDoList(false);
    },
    handleChange({ detail }) {
        this.setData({
            current: detail.key,
            showDataList: (detail.key == 0 ? this.data.readyDoList : this.data.hadDoList)
        });
    },
    onReachBottom: function () {
        if (this.data.current == 0) {
            if (this.data.readyDoPageIndex <= this.data.readyDoPageCount) {
                this.setData({
                    loading: true
                });
                this.getReadyDoList(false);
            } else {
                wx.showToast({
                    title: '没有更多数据了~',
                    icon: 'none',
                    duration: 2000
                });
            }
        } else {
            if (this.data.hadDoPageIndex <= this.data.hadDoPageCount) {
                this.setData({
                    loading: true
                });
                this.getHadDoList(false);
            } else {
                wx.showToast({
                    title: '没有更多数据了~',
                    icon: 'none',
                    duration: 2000
                });
            }
        }

    },
    //驳回
    handleClickRefuse: function (e) {
        var id = e.currentTarget.dataset.id;
        this.setData({
            selectId: id,
            visibleShowRefuse: true
        });
    },
    detailHandleRefuse: function ({ detail }) {
        if (detail.index === 0) {
            this.setData({
                selectId: 0,
                visibleShowRefuse: false
            });
        } else {
            const action = [...this.data.refuseActions];
            action[1].loading = true;

            this.setData({
                refuseActions: action
            });
            var that = this;

            //提交
            wx.request({
                url: app.globalData.api + '/CostNote/AuditHistoryInfo',
                data: {
                    token: app.globalData.userInfo.token,
                    historyId: that.data.selectId,
                    state: 2,
                    stateStr: that.data.refuseContent
                },
                method: 'GET',
                success: function (res) {
                    if (res.data.resultCode == 0) {
                        wx.showToast({
                            title: '处理成功',
                            icon: 'success',
                            duration: 2000
                        });
                        action[1].loading = false;
                        that.setData({
                            visibleShowRefuse: false,
                            refuseActions: action,
                            refuseContent: ''
                        });
                        //刷新页面
                        that.initPageData();
                    } else {
                        wx.showToast({
                            title: res.data.message,
                            icon: 'none',
                            duration: 2000
                        });
                    }
                    action[1].loading = false;
                    that.setData({
                        visibleShowRefuse: false,
                        refuseActions: action,
                        selectId: 0,
                    });
                },
                fail: function () {
                    wx.showToast({
                        title: '网络异常',
                        icon: 'none',
                        duration: 2000
                    });
                    action[1].loading = false;
                    that.setData({
                        visibleShowRefuse: false,
                        refuseActions: action,
                        selectId: 0,
                    });
                }
            });
        }
    },
    changeRefuseContent: function (e) {
        this.setData({
            refuseContent: e.detail.detail.value
        });
    },
    //展示弹窗确认窗
    handleClickShowPass: function (e) {
        var id = e.currentTarget.dataset.id;
        this.setData({
            selectId: id,
            visibleShowPass: true
        });
    },
    //取消
    handleCancelPass: function () {
        this.setData({
            selectId: 0,
            visibleShowPass: false
        });
    },
    /**
     * 确认通过
     */
    handleClickPass: function () {
        self = this;
        const action = [...self.data.passActions];
        if (action[0].loading) {
            return;
        }
        action[0].loading = true;

        self.setData({
            passActions: action
        });
        //提交
        wx.request({
            url: app.globalData.api + '/CostNote/AuditHistoryInfo',
            data: {
                token: app.globalData.userInfo.token,
                historyId: self.data.selectId,
                state: 1,
                stateStr: '同意'
            },
            method: 'GET',
            success: function (res) {
                if (res.data.resultCode == 0) {
                    wx.showToast({
                        title: '处理成功',
                        icon: 'success',
                        duration: 2000
                    });
                    //刷新页面
                    self.initPageData();
                } else {
                    wx.showToast({
                        title: res.data.message,
                        icon: 'none',
                        duration: 2000
                    });
                }
                action[0].loading = false;
                self.setData({
                    visibleShowPass: false,
                    passActions: action,
                    selectId: 0,
                });
            },
            fail: function () {
                wx.showToast({
                    title: '网络异常',
                    icon: 'none',
                    duration: 2000
                });
                action[0].loading = false;
                self.setData({
                    visibleShowPass: false,
                    passActions: action,
                    selectId: 0,
                });
            }
        });
    },
    /**
     * 预览图片
     * @param {any} e 
     */
    previewImg: function (e) {
        //获取记录的下标
        var index = e.currentTarget.dataset.index;
        var img_index = e.currentTarget.dataset.img_index;
        //所有图片
        var mediaList = this.data.showDataList[index].mediaList;
        if (mediaList && mediaList.length > 0) {
            var imgs = [];
            mediaList.forEach(item => {
                imgs.push(item.fullUrl);
            });
            wx.previewImage({
                //当前显示图片
                current: imgs[img_index],
                //所有图片
                urls: imgs
            });
        }
    },

    /**
     * 图片加载失败
     */
    imageOnloadError: function (e) {
        var that = this;
        //获取加载出错的图片下标
        var index = e.currentTarget.dataset.img_index;
        var detailIndex = e.currentTarget.dataset.index;

        console.log("图片加载出错啦,打卡下标：" + detailIndex + "图片下标：" + index);
        //后台返回的图片路径数组
        var errorMedia = this.data.showDataList[detailIndex].mediaList[index];
        if (!errorMedia.loadError) {
            var _imgUrl = this.data.showDataList[detailIndex].mediaList[index].fullUrl;
            var showDataList = this.data.showDataList;
            showDataList[detailIndex].mediaList[index].fullUrl = _imgUrl + "?_v=" + Math.random();
            showDataList[detailIndex].mediaList[index].loadError = true;
            console.log(showDataList);
            that.setData({
                showDataList: showDataList
            });
        }
    },
});